package MobileCommServer;

import jssc.*;

/**
 * <pre>
 * @Package : MobileCommServer
 * @File    : SerialComm.java
 * <pre>
 *
 * @Author  : 차재우
 * @Date    : 2013. 7. 25.
 * @Version :
 */
public class SerialComm {
	private SerialPort sp = null;
	private SerialQueue recvQueue = null;
	private SerialQueue sendQueue = null;

	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 * @param serialPort
	 */
	public SerialComm(String serialPort, SerialQueue recvQueue, SerialQueue sendQueue) {
		this.sp = new SerialPort(serialPort);
		this.recvQueue = recvQueue;
		this.sendQueue = sendQueue;

		PortOpen();
	}

	/**
	 * <pre>
	 * @Comment : RS232 포트 열기 
	 * <pre>
	 *
	 * @Method  : PortOpen
	 * @Return  : void
	 */
	private void PortOpen() {
		try {
			sp.openPort();
			sp.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
			int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;
			sp.setEventsMask(mask);
			sp.addEventListener(new SerialPortReader(sp, recvQueue));
			
			Thread serialSender = new Thread(new SerialSender(sp, sendQueue));
			serialSender.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
